package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController //@Controller + @ResponseBody的结合体
@RequestMapping("/shop") //暴露公共路径
@Api(tags = "店铺管理接口")  //swagger API
public class ShopController {

    @Autowired
    private IShopService shopService;



    /**
     * 店铺激活
     */
    @RequestMapping("/jihuo/{uuid}/{id}")
    public AjaxResult jihuo(@PathVariable("uuid") String uuid,@PathVariable("id") Long id){

        return shopService.active(uuid,id);

    }


    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻") //swagger API
    public AjaxResult settlement(@Valid @RequestBody Shop shop){
        return shopService.settlement(shop);
    }
    
    
    
    /**
     * 查询店铺分页
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询店铺分页") //swagger API
    public PageList<Shop> findPage(@RequestBody ShopQuery query){
        //查询
        return shopService.findPage(query);
    }


    /**
     * 新增/修改  ，根据id区分
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改店铺，新增没有id，修改有id") //swagger API
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){

        try {
            if (shop.getId() == null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }
    /**
     * 新增/修改  ，根据id区分
     * @return
     */
    @ApiOperation(value = "根据id删除店铺对象") //swagger API
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            shopService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }

}
